package com.keepsport.keepsportapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.keepsport.keepsportapi.dao.MallCartDao;
import com.keepsport.keepsportapi.dao.MallDao;
import com.keepsport.keepsportapi.dto.MallCartDto;
import com.keepsport.keepsportapi.dto.MallTypeDto;
import com.keepsport.keepsportapi.entity.Mall;
import com.keepsport.keepsportapi.entity.MallCart;
import com.keepsport.keepsportapi.service.intf.MallCartService;
import com.keepsport.keepsportapi.vo.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * __▁▂▃▁__ ﹀ 　　 落霞与孤骛齐飞
 * 　　﹀ _▁▂▃__ 　　 秋水共长天一色
 * 　　__▁▂▁___ ﹀ ﹋ ●
 * 　　﹋ ﹀
 * 　　█▆▅▇▆▅▄▃▄▅▆▄▅▄▃▂▃▄▆▅▇▆▄▅█
 * 　　-__~~--___-▔~~__--__~~~_----__----~~~-
 *
 * @author 1212
 * @create 2022-07-02 20:08
 */
@Service
public class MallCartServiceImpl implements MallCartService {
    @Resource
    private MallCartDao dao;
    @Resource
    private MallDao mallDao;
//用户查询
    @Override
    public R userAll(int token) {
        //校验参数
        if (token > 0) {
            //获取数据库
            List<MallCartDto> mallCartDtos = dao.selectByUser(token);
            //校验
            if (mallCartDtos!=null) {
                //查到数据 返回
                return R.ok(mallCartDtos);
            } else {
                return R.fail("请添加商品");
            }
        }
        return null;
    }
//用户增加商品
    @Override
    public R userAdd(int token, MallCart cart) {
        //校验传参
        QueryWrapper<MallCart> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("uid",token);
        wrapper1.eq("mid",cart.getMid());
        MallCart mallCart = dao.selectOne(wrapper1);
        System.out.println(dao.selectOne(wrapper1));
        System.out.println(cart.getNub());
        if (mallCart.getNub()!=null){
            System.out.println(cart.getNub()+"--getnub");

            int nub = mallCart.getNub();
            nub+=1;
            mallCart.setNub(nub);
            return R.ok(dao.updateById(mallCart));
        }


        return R.fail();


}
//用户减少商品
    @Override
    public R userDel(int token, int sid) {
        //校验
        QueryWrapper<MallCart> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("uid",token);
        wrapper1.eq("sid",sid);
        MallCart mallCart = dao.selectOne(wrapper1);
        System.out.println(dao.selectOne(wrapper1));
            if (sid > 0){
                dao.deleteById(sid);
                return R.ok();
            } else{
            return R.fail();
        }
        }




//添加购物车
    @Override
    public R add(int token, MallCart cart) {
        if (cart!=null) {
            QueryWrapper<MallCart> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("uid",token);
            wrapper1.eq("mid",cart.getMid());
            MallCart mallCart = dao.selectOne(wrapper1);
            System.out.println(dao.selectOne(wrapper1));
            System.out.println(cart.getNub());
            if (mallCart==null){
                System.out.println(cart.getNub()+"11getnub");
                cart.setUid(token);
                cart.setNub(1);
                cart.setCtime(new Date());
                return R.ok(dao.insert(cart));
            }else if (mallCart.getNub()!=null){
                System.out.println(cart.getNub()+"--getnub");

                int nub = mallCart.getNub();
                nub+=1;
                mallCart.setNub(nub);
                return R.ok(dao.updateById(mallCart));
            }

        }
        return R.fail();
    }
}
